<script setup>
import { ref } from 'vue'
import {useRouter} from 'vue-router'
const router = useRouter()
import  '@/assets/css/fancyProfile.css';
const checkRePassword = (rule, value, callback)=>{
    if(value === ''){
        callback(new Error('请再次确认密码'));
    }else if(value !== updatePasswords.value.password){
        callback(new Error('请确保两次输入密码一致'));
    }else{
        callback();
    }
};

const rules = {
    password:[
        {message:'请输入密码', trigger:'blur'},
        {min:5,max:16, message:'长度为5~16为字符', trigger:'blur'}
    ],
    rePassword:[
        {validator:checkRePassword, trigger:'blur'}
    ]
}

import {useTokenStore} from '@/stores/token.js'
const tokenStore = useTokenStore()

const updatePasswords = ref({
    oldPassword:'',
    newPassword:'',
    rePassword:''
});

import {updatePasswordService} from '@/api/user.js'
import {ElMessage} from 'element-plus'
const updatePassword = async ()=>{
    let result = await updatePasswordService(updatePasswords.value, tokenStore.token);
    ElMessage.success(result.msg? result.msg:'修改成功')
    tokenStore.removeToken()
    router.push('/login')
}

</script>
<template>
    <el-card class="page-container">
        <template #header>
            <div class="header">
                <span>基本资料</span>
            </div>
        </template>
        <el-row>
            <el-col :span="12">
                <el-form :model="updatePasswords" :rules="rules" label-width="100px" size="large">
                    <el-form-item label="旧密码" prop="password">
                        <el-input  v-model="updatePasswords.oldPassword"></el-input>
                    </el-form-item>
                    <el-form-item label="新密码" prop="password">
                        <el-input  type="password" placeholder="请输入密码" v-model="updatePasswords.newPassword"></el-input>
                    </el-form-item>
                    <el-form-item label="确认密码" prop="rePassword">
                        <el-input  type="password" placeholder="请再次输入密码" v-model="updatePasswords.rePassword"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="updatePassword">提交修改</el-button>
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>
    </el-card>
</template>